
// https://leetcode.cn/problems/combination-sum/description/

class Solution
{
    vector<vector<int>> res;
    vector<int> path;
    int sum;
    int t;
public:
    vector<vector<int>> combinationSum(vector<int>& v, int target)
    {
        t = target;
        dfs(v, 0);
        return res;
    }

    void dfs(vector<int>& v, int idx)
    {
        if (sum == t)
        {
            res.push_back(path);
            return;
        }
        if (sum > t)
            return;

        for (int i = idx; i < v.size(); ++i)
        {
            sum += v[i];
            path.push_back(v[i]);
            dfs(v, i);
            sum -= v[i];
            path.pop_back();
        }
    }
};